package com.wisemoresystem.seal.action.mac.keep;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_mac;
import com.wisemoresystem.seal.model.t_mac_move_log;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.MacMoveLogService;
import com.wisemoresystem.seal.service.MacService;

/**
 * 维护
 * 
 * @author 冯晓东
 *
 */
@Controller("macKeep")
@Scope("prototype")
@RequestMapping("/ix/mac/keep")
public class MacKeepAction extends ProjBaseAction {

	@Autowired
	private MacService macService;
	@Autowired
	private MacMoveLogService macLogService;
	/**
	 * 印控仪维护列表
	 * @param srh_name 印控仪id
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> macList = macService.selectStartingList(
				srh_name, page);

		ModelAndView mv = new ModelAndView("/jsp/mac/keep/list");
		mv.addObject("macList", macList);
		mv.addObject("srh_name", srh_name);
		return mv;
	}
	/**
	 * 印控仪详细信息
	 * @param mac_id 印控仪编号
	 * @return
	 */
	@RequestMapping("/detail")
	public ModelAndView detail(String mac_id) {

		t_mac mac = oc.findObjById(mac_id, t_mac.class);
		t_bank bank = oc.findObjById(mac.getBank_id(), t_bank.class);
		t_user user = oc.findObjById(mac.getUser_id(), t_user.class);
		t_user userDis = null;
		t_mac_move_log moveDis = macLogService.move(mac_id, "分发");
		if (moveDis != null) {
			userDis = oc.findObjById(moveDis.getUser_id(), t_user.class);
		} else {
			userDis = new t_user();
			userDis.setUname("此设备还未被分发过");
		}

		t_user userRec = null;
		t_mac_move_log moveRec = macLogService.move(mac_id, "接收");
		if (moveRec != null) {
			userRec = oc.findObjById(moveRec.getUser_id(), t_user.class);
		} else {
			userRec = new t_user();
			userRec.setUname("此设备未曾接被收过");
		}

		t_user userTra = null;
		t_mac_move_log moveTra = macLogService.move(mac_id, "调拨");
		if (moveTra != null) {
			userTra = oc.findObjById(moveTra.getUser_id(), t_user.class);
		} else {
			userTra = new t_user();
			userTra.setUname("此设备未曾调拨");
		}

		ModelAndView mv = new ModelAndView("/jsp/mac/keep/detail");
		mv.addObject("mac", mac);
		mv.addObject("bank", bank);
		mv.addObject("user", user);// 印控仪录入用户
		mv.addObject("userDis", userDis);// 印控仪分发用户
		mv.addObject("moveDis", moveDis);// 备注
		mv.addObject("userRec", userRec);// 印控仪接收用户
		mv.addObject("moveRec", moveRec);// 备注
		mv.addObject("userTra", userTra);// 印控仪调拨用户
		mv.addObject("moveTra", moveTra);// 备注
		return mv;
	}

}
